package Thread;
//使用库函数中的循环标记中止线程
public class ThreadDemo6 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(){
            @Override
            public void run() {
                //默认情况下isInterrupted()为false
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println("线程正在运行");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();
        Thread.sleep(3000);
        //在主线程中通过interr()来设置标记位
        //如果线程正在运行interr()会将Thread.currentThread().isInterrupted()设置为true;
        //如果线程正在sleep/wait/等待锁...此时就会触发InterruptedException
        //所以要加break才能让线程正常中断
        t.interrupt();
    }
}
